//
//  YXWalletHeaderView.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/22.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXWalletHeaderView.h"
#import "YXBankModel.h"
@interface YXWalletHeaderView ()
@property (nonatomic ,strong) UILabel *titleLab;

@property (nonatomic ,strong) UIView *lineView;
@property (nonatomic ,strong) UIButton *replaceBtn;
@property (nonatomic ,strong) UIView *bottomLineView;
@end

@implementation YXWalletHeaderView

-(UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab  = [UILabel setLabelWithText:@"到账储蓄卡(默认实名认证时添加的银行卡)" fontSize:16 color:color_textOne];
        _titleLab.textAlignment = NSTextAlignmentCenter;
        _titleLab.adjustsFontSizeToFitWidth = YES;
    }
    return _titleLab;
}

- (UILabel *)cardNumberLab {
    if (!_cardNumberLab) {
        _cardNumberLab = [UILabel setLabelWithText:@"" fontSize:18 color:color_globalColor];
        _cardNumberLab.textAlignment = NSTextAlignmentCenter;
        _cardNumberLab.adjustsFontSizeToFitWidth = YES;
        _cardNumberLab.attributedText = [self bankCardNoAttriStr];
    }
    return _cardNumberLab;
}
- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = color_lineColor;
    }
    return _lineView;
}

- (UIButton *)replaceBtn {
    if (!_replaceBtn) {
        _replaceBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_replaceBtn setTitle:@"更换银行卡" forState:(UIControlStateNormal)];
        [_replaceBtn setTitleColor:color_textOne forState:(UIControlStateNormal)];
        _replaceBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_replaceBtn addTarget:self action:@selector(releaseBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _replaceBtn;
}

- (void)releaseBtnAction {
    if (self.releaseBtnClickBlock) {
        self.releaseBtnClickBlock();
    }
}

- (UIView *)bottomLineView {
    if (!_bottomLineView) {
        _bottomLineView = [[UIView alloc] init];
        _bottomLineView.backgroundColor = color_lineColor;
    }
    return _bottomLineView;
}

- (void)setCardNumber:(NSString *)cardNumber {
    _cardNumber = cardNumber;
//    _cardNumberLab.text = [UILabel formateBankNum:_cardNumber];
    
    _cardNumberLab.attributedText = [self bankCardNoAttriStr1];


}

- (void)setModel:(YXBankModel *)model {
    _model = model;
    
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setup];
    }
    return self;
}

- (void)setup {
    [self addSubview:self.titleLab];
    [self addSubview:self.cardNumberLab];
    [self addSubview:self.lineView];
    [self addSubview:self.replaceBtn];
    [self addSubview:self.bottomLineView];
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    YXWeakSelf
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.mas_top).offset(30);
        make.left.equalTo(weakSelf.mas_left).offset(0);
        make.right.equalTo(weakSelf.mas_right).offset(0);
        [_titleLab sizeToFit];
    }];
    
    [_cardNumberLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.titleLab.mas_bottom).offset(10);
        make.left.equalTo(weakSelf.mas_left).offset(0);
        make.right.equalTo(weakSelf.mas_right).offset(0);
        [_titleLab sizeToFit];
    }];
    
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.cardNumberLab.mas_bottom).offset(20);
        make.left.equalTo(weakSelf.mas_left).offset(0);
        make.right.equalTo(weakSelf.mas_right).offset(0);
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, 1));
    }];
    
    [_replaceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.mas_right).offset(-10);
        make.top.equalTo(weakSelf.lineView.mas_bottom).offset(10);
        [_replaceBtn sizeToFit];
    }];
    
    [_bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(weakSelf.mas_bottom).offset(0);
        make.left.equalTo(weakSelf.mas_left).offset(0);
        make.right.equalTo(weakSelf.mas_right).offset(0);
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, 1));
    }];
}
-(NSMutableAttributedString *)bankCardNoAttriStr
{
    NSString *str = [YXUserInfoManager getUserInfo].idCard;
    NSString *bankcardnoSub = [str substringFromIndex:str.length- 4];
//    NSString *bankcardnoSub = @"7705";
    NSMutableAttributedString *attriStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"＊＊＊＊ ＊＊＊＊ ＊＊＊＊ %@",bankcardnoSub]];
    [attriStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.f] range:[attriStr.string rangeOfString:@"＊＊＊＊ ＊＊＊＊ ＊＊＊＊"]];
    [attriStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20.f] range:[attriStr.string rangeOfString:bankcardnoSub]];
    return attriStr;
}

-(NSMutableAttributedString *)bankCardNoAttriStr1
{
    NSString *str = _cardNumber;
    NSString *bankcardnoSub = [str substringFromIndex:str.length- 4];
    //    NSString *bankcardnoSub = @"7705";
    NSMutableAttributedString *attriStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"＊＊＊＊ ＊＊＊＊ ＊＊＊＊ %@",bankcardnoSub]];
    [attriStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.f] range:[attriStr.string rangeOfString:@"＊＊＊＊ ＊＊＊＊ ＊＊＊＊"]];
    [attriStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20.f] range:[attriStr.string rangeOfString:bankcardnoSub]];
    return attriStr;
}

@end
